package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.vo.SaveAttrInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lyd
 * @Date:2023/8/15 16:56
 * @Version:1.0
 * @Description
 **/
@RestController
@RequestMapping("/admin/product")
public class BaseAttrController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    /**
     * 查询分类id获取平台属性
     * @param c1Id
     * @param c2Id
     * @param c3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(
            @PathVariable("category1Id") Long c1Id,
            @PathVariable("category2Id") Long c2Id,
            @PathVariable("category3Id") Long c3Id
    ) {
        List<BaseAttrInfo> list = baseAttrInfoService.getAttrInfoList(c1Id, c2Id, c3Id);
        return Result.ok(list);
    }

    /**
     * 添加平台属性
     *
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody SaveAttrInfoVo vo) {
        if (vo.getId() != null) {
            //修改
            baseAttrInfoService.updateAttrInfo(vo);
        }else {
            //新增
            baseAttrInfoService.saveAttrInfo(vo);
        }

        return Result.ok();
    }

    /**
     * 根据平台属性ID查找所有的平台属性对象数据
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {
        List<BaseAttrValue> list = baseAttrValueService.lambdaQuery()
                .eq(BaseAttrValue::getAttrId, attrId)
                .list();
        return Result.ok(list);
    }

}
